package com.penghk.springbootdemo.rabbitMQ.spring;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;

public class Consumer {

    public static void main(String[] args) throws InterruptedException {

        ConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1", 5672);

        //消息监听
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        Object listener = new Object() {
            public void handleMessage(String foo) {
                System.out.println(" [x] Received '" + foo + "'");
            }
        };
        MessageListenerAdapter adapter = new MessageListenerAdapter(listener);
        container.setMessageListener(adapter);
        container.setQueueNames("springQueue");
        container.start();

        while (true) {
            Thread.sleep(1000);
            System.out.println("main thread running");
        }
    }
}
